package edu.lsnu.equipment_management_system.service.impl;

import edu.lsnu.equipment_management_system.exception.InsertIntoEquipmentRepairTableException;
import edu.lsnu.equipment_management_system.exception.InsertIntoEquipmentUseRecordTableException;
import edu.lsnu.equipment_management_system.mapper.insert.*;
import edu.lsnu.equipment_management_system.mapper.select.paging.EquipmentInformationMapperWithPaging;
import edu.lsnu.equipment_management_system.mapper.update.ChangeUseStatusMapper;
import edu.lsnu.equipment_management_system.pojo.domain.UseStatusConstant;
import edu.lsnu.equipment_management_system.pojo.po.entity.*;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.UserNoEquipmentNoAndDateVo;
import edu.lsnu.equipment_management_system.service.TableOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author mzy
 * @Description 数据库表的操作：插入
 * @date 2018/11/26
 */
@Service
@Transactional(propagation= Propagation.REQUIRED)
public class TableOperationServiceImpl implements TableOperationService {
    @Autowired
    UseStatusConstant useStatusConstant;
    @Autowired
    ChangeUseStatusMapper changeUseStatusMapper;

    @Autowired
    AdminAddUserMapper adminAddUserMapper;
    @Autowired
    EquipmentDamageTableMapper equipmentDamageTableMapper;
    @Autowired
    EquipmentInformationTableMapper equipmentInformationTableMapper;
    @Autowired
    EquipmentRepairTableMapper equipmentRepairTableMapper;
    @Autowired
    EquipmentTransferTableMapper equipmentTransferTableMapper;
    @Autowired
    EquipmentUseRecordTableMapper equipmentUseRecordTableMapper;
    @Autowired
    EquipmentInformationMapperWithPaging equipmentInformationMapperWithPaging;
    /**
     * @Description 插入到用户表
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [userTablePo]
     */
    @Override
    public boolean insertIntoUserTable(UserTablePo userTablePo) {
        int count = adminAddUserMapper.insertNewUser(userTablePo);

        return count != 0;
    }

    @Override
    public boolean insertIntoEquipmentDamageTable(EquipmentDamageTablePo equipmentDamageTablePo) {
        int count = equipmentDamageTableMapper.insertEquipmentDamageTable(equipmentDamageTablePo);
        // 设备状态变成报废
        changeUseStatusMapper.changeUseStatus(equipmentDamageTablePo.getEquipmentNo(),
                useStatusConstant.getDamage());
        return count != 0;
    }

    @Override
    public boolean insertIntoEquipmentInformationTable(EquipmentInformationTablePo equipmentInformationTablePo) {
        int count = equipmentInformationTableMapper.insertEquipmentInformationTable(equipmentInformationTablePo);
        // 设备状态变成备用
        changeUseStatusMapper.changeUseStatus(equipmentInformationTablePo.getEquipmentNo(),
                useStatusConstant.getSpare());
        return count != 0;
    }

    @Override
    public boolean insertIntoEquipmentRepairTable(EquipmentRepairTablePo equipmentRepairTablePo) throws InsertIntoEquipmentRepairTableException {
        String equipmentNo = equipmentRepairTablePo.getEquipmentNo();
        String userNo = equipmentRepairTablePo.getUserNo();
        Date repairDate = equipmentRepairTablePo.getRepairDate();
        UserNoEquipmentNoAndDateVo userNoEquipmentNoAndDateVo = new UserNoEquipmentNoAndDateVo();

        userNoEquipmentNoAndDateVo.setEquipmentNo(equipmentNo);
        userNoEquipmentNoAndDateVo.setUserNo(userNo);
        userNoEquipmentNoAndDateVo.setDate(repairDate);
        // 同一个天一个设备不能维修多次！
        int res = equipmentInformationMapperWithPaging.getEquipmentRepairInsertValueIsExist(userNoEquipmentNoAndDateVo);
        if (res != 0) {
            throw new InsertIntoEquipmentRepairTableException();
        }

        int count = equipmentRepairTableMapper.insertEquipmentRepairTable(equipmentRepairTablePo);
        // 设备状态变成待修
        changeUseStatusMapper.changeUseStatus(equipmentRepairTablePo.getEquipmentNo(),
                useStatusConstant.getRepair());
        return count != 0;
    }

    @Override
    public boolean insertIntoEquipmentTransferTable(EquipmentTransferTablePo equipmentTransferTablePo) {
        int count = equipmentTransferTableMapper.insertEquipmentTransferTable(equipmentTransferTablePo);
        if ("调出".equals(equipmentTransferTablePo.getTranserOr())) {
            // 设备状态变成调出
            changeUseStatusMapper.changeUseStatus(equipmentTransferTablePo.getEquipmentNo(),
                    useStatusConstant.getTransferOut());
        }
        return count != 0;
    }

    @Override
    public boolean insertIntoEquipmentUseRecordTable(EquipmentUseRecordTablePo equipmentUseRecordTablePo) throws InsertIntoEquipmentUseRecordTableException {
        String equipmentNo = equipmentUseRecordTablePo.getEquipmentNo();
        String userNo = equipmentUseRecordTablePo.getUserNo();
        Date transDate = equipmentUseRecordTablePo.getBeginDate();
        UserNoEquipmentNoAndDateVo userNoEquipmentNoAndDateVo = new UserNoEquipmentNoAndDateVo();

        userNoEquipmentNoAndDateVo.setEquipmentNo(equipmentNo);
        userNoEquipmentNoAndDateVo.setUserNo(userNo);
        userNoEquipmentNoAndDateVo.setDate(transDate);
        // 同一天一个设备不能转让多次！
        int res = equipmentInformationMapperWithPaging.getEquipmentUseRecordInsertValueIsExist(userNoEquipmentNoAndDateVo);
        if (res != 0) {
            throw new InsertIntoEquipmentUseRecordTableException();
        }


        int count = equipmentUseRecordTableMapper.insertEquipmentUseRecord(equipmentUseRecordTablePo);
        // 设备状态变成在用
        changeUseStatusMapper.changeUseStatus(equipmentUseRecordTablePo.getEquipmentNo(),
                useStatusConstant.getUsing());
        return count != 0;
    }
}
